package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class InsertSqlCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public InsertSqlCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        String clumns="";
        String values="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    if(!wsdtField.isIncrement()) {
                        if(wsdtField.getWsdtFieldDesc()!=null&&!wsdtField.getWsdtFieldDesc().isEdit()){//不编辑字段
                            continue;
                        }
                        clumns += "`"+wsdtField.getColumnName() + "`,";
                        values += "#{" + wsdtField.getFieldName() + "},";
                    }
                }
            }
            if(!"".equals(values)){
                clumns=clumns.substring(0,clumns.length()-1);
                values=values.substring(0,values.length()-1);
                code+="insert into "+wsdtEntity.getTableName()+" ("+clumns+") values ("+values+")";
            }
        }
        return code;
    }
}
